﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.Entity;
using Domain.Abstract;
using System.Data;

namespace Domain.Concrete
{
    public class EFVideoRepository : IVideoRepository
    {
        public EFDbContext Context { get; set; }

        public EFVideoRepository()
        {
            Context = EFDbContext.Instance;
        }

        public EFVideoRepository(EFDbContext context)
        {
            Context = context;
        }

        public IQueryable<Video> Videos
        {
            get { return Context.Videos; }
        }

        public void SaveVideo(Video video)
        {
            if (video.VideoID == 0)
            {
                Context.Videos.Add(video);
            }
            else
            {
                Context.Entry(video).State = EntityState.Modified;
            }

            Context.SaveChanges();
        }

        public void RemoveVideo(Video video)
        {
            var dbVideo = Videos.SingleOrDefault(v => v.VideoID == video.VideoID);

            if (dbVideo != null)
            {
                Context.Videos.Remove(dbVideo);
            }

            Context.SaveChanges();
        }

        public Video SearchVideos(string fileName)
        {
            
            return Context.Videos.FirstOrDefault(v => v.VideoName.Equals(fileName));
        }
    }
}
